package com.branches.imgs;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wujian
 * @date 2024/5/22
 */
public class CombineItem {
    private int x;

    private int y;

    private int height;

    private int width;

    private OCRExamItem ocrExamItem;

    public OCRExamItem getOcrExamItem() {
        return ocrExamItem;
    }

    public void setOcrExamItem(OCRExamItem ocrExamItem) {
        this.ocrExamItem = ocrExamItem;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    private List<RecogResult.Word> wordList;

    public List<RecogResult.Word> getWordList() {
        return wordList;
    }

    public void setWordList(List<RecogResult.Word> wordList) {
        this.wordList = wordList;
    }

    public void addWord(RecogResult.Word word) {
        if(!word.isUsed()) {
            RecogResult.Word.Location location = word.getLocation();
            if(isInclude(location)) {
                if(wordList == null) {
                    wordList = new ArrayList<>();
                }
                word.setUsed(true);
                wordList.add(word);
            }
        }
    }

    public boolean isInclude(RecogResult.Word.Location location) {
        // 左边的顶点
        int tlxdis = getX() - location.getLeft();
        int tlydis = getY() - location.getTop();
        int rtdis = getX() + getWidth() - location.getWidth() - location.getLeft();
        int bldis = getY() + getHeight() - location.getHeight() - location.getTop();

        return tlxdis <= 10
                && tlydis <= 10
                && rtdis >= -10
                && bldis >= -10;
    }


    public void parse() {
        if(this.getWordList() != null) {
            String value = ocrExamItem.getRecognitionValue();
            for (RecogResult.Word word : this.getWordList()) {
                value += " ";
                value += word.getWords();
            }
            ocrExamItem.setRecognitionValue(value);
            System.out.println( ocrExamItem.getFile().getName() + ", " + ocrExamItem.getRecognitionValue());
        }
    }
}
